package cn.zhouchao.system.controller;

import cn.zhouchao.basic.util.AjaxResult;
import cn.zhouchao.basic.util.PageList;
import cn.zhouchao.system.domain.Permission;
import cn.zhouchao.system.query.PermissionQuery;
import cn.zhouchao.system.service.IPermissionService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/per")
public class PermissionController {
    /**
     * 引入Service接口
     */
    @Autowired
    public IPermissionService iPermissionService;

    /**
     * Restful 风格 路径变量
     * http://localhost:8080/per
     * 查询所有权限
     *
     * @return
     */
    @GetMapping()
    public List<Permission> findPerAll() {
        return iPermissionService.findAll();
    }

    /**
     * 查询单个权限
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询详情")
    public Permission findById(@ApiParam(value = "部门id", required = true) @PathVariable("id") Long id) {
        return iPermissionService.findById(id);

    }

    /**
     * 添加或者修改权限
     *
     * @param permission
     * @return
     */
    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Permission permission) {
        if (null != permission.getId()) {
            // 修改
            iPermissionService.update(permission);
        } else {
            // 添加
            iPermissionService.add(permission);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        iPermissionService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 批量删除权限
     *
     * @return
     */
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        iPermissionService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     */
    @PostMapping()
    public PageList<Permission> queryByPage(@RequestBody PermissionQuery permissionQuery) {
        return iPermissionService.queryByPage(permissionQuery);
    }
}
